package org.hcyspartnoc.luoxin.controller.chat;

import jakarta.validation.Valid;
import jakarta.validation.constraints.Max;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import org.hcyspartnoc.luoxin.common.core.AjaxResult;
import org.hcyspartnoc.luoxin.common.core.PageBean;
import org.hcyspartnoc.luoxin.common.core.annotation.Log;
import org.hcyspartnoc.luoxin.common.core.annotation.PreAuthorize;
import org.hcyspartnoc.luoxin.common.core.enums.BusinessType;
import org.hcyspartnoc.luoxin.common.exception.chat.ChatMessageException;
import org.hcyspartnoc.luoxin.common.exception.framework.AuthException;
import org.hcyspartnoc.luoxin.pojo.dto.chat.ChatMessageLikeDto;
import org.hcyspartnoc.luoxin.pojo.po.chat.ChatMessagePo;
import org.hcyspartnoc.luoxin.pojo.vo.chat.ChatMessageVo;
import org.hcyspartnoc.luoxin.service.chat.IChatMessageService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/chat/message")
public class ChatMessageController {
    @Resource
    private IChatMessageService chatMessageService;

    // 分页获取聊天消息
    @Log(title = "聊天·消息·查询·分页·token", businessType = BusinessType.OTHER)
    @PreAuthorize(auth = "chat:message:query:page:token", descr = "分页查询聊天消息")
    @GetMapping("/query/page/token")
    public AjaxResult queryPageByToken(@Min(value = 1, message = "当前页不能小于1")
                                       @RequestParam(defaultValue = "1") int currentPage,
                                       @Min(value = PageBean.MIN_PAGE_SIZE, message = "每页大小" + PageBean.MIN_PAGE_SIZE + "-" + PageBean.MAX_PAGE_SIZE)
                                       @Max(value = PageBean.MAX_PAGE_SIZE, message = "每页大小" + PageBean.MIN_PAGE_SIZE + "-" + PageBean.MAX_PAGE_SIZE)
                                       @RequestParam(defaultValue = "5") int pageSize,
                                       @NotBlank(message = "会话id不能为空")
                                       @RequestParam String sessionId) throws AuthException {
        PageBean<ChatMessagePo, ChatMessageVo> page = chatMessageService.queryPageByToken(currentPage, pageSize, sessionId);
        return AjaxResult.success(page);
    }

    @Log(title = "聊天·消息·修改·点赞", businessType = BusinessType.UPDATE)
    @PreAuthorize(auth = "chat:message:modify:like", descr = "点赞")
    @PutMapping("/modify/like")
    public AjaxResult modifyLike(@Valid @RequestBody ChatMessageLikeDto chatMessageLikeDto) throws ChatMessageException, AuthException {
        chatMessageService.modifyLike(chatMessageLikeDto);
        return AjaxResult.successMsg();
    }

    @Log(title = "聊天·消息·删除", businessType = BusinessType.DELETE)
    @PreAuthorize(auth = "chat:message:remove", descr = "删除消息")
    @DeleteMapping("/remove/{messageId:\\d+}")
    public AjaxResult removeMessage(@NotNull(message = "id不能为空") @PathVariable Long messageId) throws AuthException, ChatMessageException {
        chatMessageService.removeMessage(messageId);
        return AjaxResult.successMsg();
    }
}
